package service

import (
	"apitestAfterGo/dao"
	"apitestAfterGo/domain"
	"apitestAfterGo/dto"
)

type interFaceService struct {
	GetAllInterfaceByPage   func(dto dto.InterfacePageInputDto) ([]domain.Interface, int)
	AddInterface            func(dto domain.Interface) bool
	DeleteInterFaceById     func(id int) bool
	UpdateInterfaceById     func(dto domain.Interface) bool
	FindInterfaceById       func(id int) domain.Interface
	FindInterfaceBySystemId func(id int) []domain.Interface
}

var InterFaceServiceImpl interFaceService

func init() {
	InterFaceServiceImpl = interFaceService{
		GetAllInterfaceByPage: func(dto dto.InterfacePageInputDto) ([]domain.Interface, int) {
			dto.Current = (dto.Current - 1) * dto.PageSize
			var interfaces []domain.Interface
			dao.GormDb.Debug().Where("system_id = ? and request like ? and path like ? and name like ?", dto.SystemId, "%"+dto.Request+"%", "%"+dto.Path+"%", "%"+dto.Name+"%").Limit(dto.PageSize).Offset(dto.Current).Find(&interfaces)
			var count int64
			dao.GormDb.Debug().Model(&domain.Interface{}).Select("count(1)").Where("system_id = ?", dto.SystemId).Count(&count)
			return interfaces, int(count)
		},
		AddInterface: func(dto domain.Interface) bool {
			dao.GormDb.Debug().Create(&dto)
			return true
		},
		DeleteInterFaceById: func(id int) bool {
			dao.GormDb.Debug().Where("id = ?", id).Delete(&domain.Interface{})
			return true
		},
		UpdateInterfaceById: func(dto domain.Interface) bool {
			dao.GormDb.Debug().Model(&dto).Select("*").Omit("CreateBy", "CreatedAt").Updates(dto)
			return true
		},
		FindInterfaceById: func(id int) domain.Interface {
			var interFace domain.Interface
			dao.GormDb.Debug().Where("id = ?", id).Find(&interFace)
			return interFace
		},
		FindInterfaceBySystemId: func(id int) []domain.Interface {
			var interFaces []domain.Interface
			dao.GormDb.Debug().Where("system_id = ?", id).Find(&interFaces)
			return interFaces
		},
	}
}
